﻿using CefSharp;
using CefSharp.Wpf;
using System;
using System.IO;
using System.Windows;

namespace GetMan.CEF
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        ChromiumWebBrowser CefBrowser { get; set; } = null;

        public string HtmlFile
        {
            get; set;
        }
        public string ProxyIP
        {
            get; set;
        }

        public MainWindow() : this(400, 300)
        {
            InitializeComponent();
        }

        public MainWindow(double width, double height)
        {
            try
            {
                Width = width;
                Height = height;
                Loaded += Window_Loaded;
            }
            catch
            {

            }
        }

        void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                try
                {
                    string cachePath = "cache";
                    string cacheFullPath = System.IO.Path.GetFullPath(cachePath);
                    if (!Directory.Exists(cachePath))
                    {
                        Directory.CreateDirectory(cachePath);
                    }

                    //var cefSettings = new CefSettings { CachePath = cacheFullPath, };

                    //if (!string.IsNullOrEmpty(ProxyIP))
                    //    cefSettings.CefCommandLineArgs.Add("proxy-server", ProxyIP);
                    var cefSettings = new CefSettings();
                    Cef.Initialize(cefSettings);
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"{ex}");
                }

                CefBrowser = new ChromiumWebBrowser();
                CefBrowser.Width = Width;
                CefBrowser.Height = Height;
                string htmlUrl = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resources/CodeMirrorEdit.html");
                CefBrowser.LoadUrl(htmlUrl);
                root_grid.Children.Add(CefBrowser);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"导航错误！{ex}");
            }
        }



    }
}
